﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfContrib.Errors;
using Contracts;

namespace WcfContrib.Showcase.Service
{
    class DoOperationAuthorizationAttribute : WcfContrib.Security.SecuredOperationCallAttribute
    {
        protected override bool CanExecute(string operationName, object[] inputs)
        {
            Helpers.WriteLine(ConsoleColor.Yellow, "DoOperationAuthorizationAttribute.CanExecute()");

            //allow execution only for modules with id higher than 5 AND foo objects with id higher than 5
            Foo foo;
            int moduleId;
            if (TryGetInput(inputs, out moduleId) && TryGetInput(inputs, out foo))
                return moduleId > 5 && foo.Id > 5;

            return false;
        }

        protected override Exception GetUnauthorizedException()
        {
            //you can control the thrown exception..
            //throw new System.ServiceModel.FaultException("AAA");

            return base.GetUnauthorizedException();
        }
    }
}
